home *** CD-ROM | disk | FTP | other *** search
/ Gold Medal Software 3 / Gold Medal Software - Volume 3 (Gold Medal) (1994).iso / tutor / pcl61a.arj / PART3.EXE / BATCH.TUT next >
Text File  |  1993-08-16  |  51KB  |  1,052 lines

  1.       
  2.        ----------------------------------------------------------------
  3.  
  4.               BATCH FILES FOR EFFICIENCY - GETTING REAL WORK DONE 
  5.  
  6.        ----------------------------------------------------------------
  7.  
  8.        Batch files are one of the hidden treasures within your 
  9.        computer. Let's face it, learning and using DOS commands is a 
  10.        test of patience, memory and stamina. Batch files are the secret 
  11.        weapon which can boost your computer into the fast lane of work 
  12.        productivity. 
  13.        
  14.        Batch files are small software programs which you can prepare in 
  15.        the space of only five or ten minutes which automate a variety 
  16.        of tasks and customize the computer to your work style. The 
  17.        bottom line is that batch files can preserve your sanity in the 
  18.        face of arcane, easily-forgotten DOS commands to provide you 
  19.        with a menu system for your hard drive, transfer and backup 
  20.        files, provide security, start other software programs, activate 
  21.        your printer and much more. 
  22.        
  23.        Amazingly, batch files require that you become familiar with 
  24.        only eight commands in addition to the normal DOS commands. 
  25.        Working with batch files means you are programming in the most 
  26.        literal sense - let's take a tour of the small miracles called 
  27.        batch files . . . 
  28.        
  29.        A batch file is little more than a list of DOS commands plus 
  30.        eight special batch commands. These commands are stored on your 
  31.        floppy or hard disk in an ordinary text file such as produced by 
  32.        your word processor or text editor. If you can type a letter to 
  33.        a friend, you can prepare a batch file! 
  34.               
  35.        Several time-saving batch file examples which provide real work 
  36.        power to your everyday computer problems are presented at the 
  37.        end of this tutorial. We will also examine the eight basic batch 
  38.        file commands as well as the AUTOEXEC.BAT file which starts your 
  39.        computer each morning. In addition we will present some simple 
  40.        tricks for managing your printer with batch files.
  41.           
  42.        One way to think about a batch file is that it takes the 
  43.        place of your keyboard and issues commands one after another 
  44.        until it reaches a conclusion. Batch files operate line by line 
  45.        and are read directly from the disk which makes them a little 
  46.        slow, but nevertheless useful and flexible. You can eliminate 
  47.        repetitious keyboard tasks by using batch files. Let's look at a 
  48.        short batch file . . .
  49.           
  50.        Each line of a batch file contains one instruction or operation 
  51.        per line which the computer is to perform. Below is the listing 
  52.        of a simple batch file example. Don't worry about understanding 
  53.        it yet, simply note that each instruction is a DOS command on a 
  54.        separate line. The list in the left column is the actual batch 
  55.        file, while the explanation in the right column is NOT part of 
  56.        the batch file, only a helpful column of comments. 
  57.  
  58.         EXAMPLE BATCH FILE                EXPLANATION
  59.                |                               |
  60.               date                         date displayed 
  61.               time                         time displayed
  62.               ver                          DOS version displayed
  63.               dir a:/p                     directory of a: floppy 
  64.                                            displayed with a pause
  65.  
  66.        The primary use of batch files is to automate sequences or 
  67.        instructions which you use frequently. A batch file always has 
  68.        the extension BAT. A batch file might for instance be named
  69.        MENU.BAT, CAR.BAT, INSTALL.BAT or MONEY.BAT.
  70.  
  71.        Each line in a batch file is a separate command and is performed 
  72.        in sequence as if you had typed in the command from your 
  73.        keyboard at the DOS prompt. In addition to the usual DOS 
  74.        commands, batch files can also contain additional special 
  75.        commands to provide truly sophisticated program structures which 
  76.        include decision branching and even repetitions of commands. 
  77.        In addition, batch files may have special parameters or inputs 
  78.        passed to them at the time you run the batch file from the DOS 
  79.        command line or prompt. 
  80.        
  81.        A batch file is run or started by typing the file name without 
  82.        the extension. This of course also applies to files ending with 
  83.        file extensions EXE or COM as well as BAT.
  84.  
  85.        Example: A>hello     (Then pressing enter or return key)
  86.        This starts the file   hello.bat hello.com or hello.exe
  87.  
  88.        Example: C>whoops    (Then pressing enter or return key)
  89.        This starts the file  whoops.exe whoops.bat or whoops.com
  90.        
  91.        There are several ways to abort or terminate any batch file in 
  92.        progress. 1) Issue the break command which uses the two key 
  93.        combination CONTROL-BREAK (hold down the control or CTRL key 
  94.        then press the break key) or you can 2) Tap CTRL-SCROLL LOCK 
  95.        keys or 3) Tap CTRL-C keys. 
  96.  
  97.        There are many ways to prepare a batch file, all of which use 
  98.        simple methods of text editing or word processing:  1) Use the 
  99.        DOS COPY CON (copy console) command. 2) Use the older EDLIN line 
  100.        editor available within older versions of DOS. 3) Use the newer 
  101.        DOS EDIT text editor available in DOS version 5.0  and 6.0. This 
  102.        is probably the most convenient. 4) Or use any word processor 
  103.        (e.g., Microsoft Word, Wordperfect, PC-Write) whose output has 
  104.        been set to ASCII or pure text output - many word processors use 
  105.        a "save as" file option to select pure ASCII output. See your 
  106.        word processor reference book index under ASCII file saving. 
  107.        
  108.        Let's prepare a batch file: 
  109.  
  110.        First make sure you have a formatted disk in your disk drive and 
  111.        DOS is displaying a DOS prompt such as A> or C>. We need a disk 
  112.        in order to save our batch file.
  113.        
  114.        We will be using the command COPY CON (copy data from the 
  115.        CONsole) command. We could also use any ASCII (plain english) 
  116.        text word processor (e.g., Wordperfect) or even EDLIN on your 
  117.        DOS disk. Note that you can use either upper or lower case to 
  118.        prepare batch files (capitals or small letters.) Using COPY CON 
  119.        is like using a small typewriter to prepare your batch file. 
  120.  
  121.        Type the following list carefully at the DOS prompt:
  122.  
  123.        copy con blink.bat     (press enter - cursor skips to new line)
  124.        echo Hello there       (press enter)
  125.        ver                    (press enter)
  126.        date                   (press enter)
  127.        dir/p                  (press enter)
  128.        ^Z                     (press F6 OR your can press control key 
  129.                               AND Z key, then press enter)
  130.  
  131.        When done, you'll have prepared a batch file of DOS commands 
  132.        named blink.bat. Run the batch file by typing this at the DOS 
  133.        prompt: 
  134.                           blink    (then press enter key)
  135.        
  136.        Although using the COPY CON method method of preparing a batch 
  137.        file works, users of DOS 5.0 and 6.0 will find it more 
  138.        convenient to use the built in DOS "miniword processor" called 
  139.        EDIT. Just type the command EDIT  <then press enter/return key> 
  140.        to load EDIT. Then use the convenient popdown menus within EDIT 
  141.        to create a file or load a file from the disk. 
  142.  
  143.        WARNING! Be careful when preparing batch files since you will 
  144.        automatically overwrite and destroy any PREXISTING batch files 
  145.        of the same name! It may be better to make a backup copy of the 
  146.        existing batch file (or rename it temporarily with the REN 
  147.        command) and then proceed. A classic beginner mistake is to 
  148.        tinker with the crucial AUTOEXEC.BAT file without saving a 
  149.        backup copy first! If you destroy AUTOEXEC.BAT with your 
  150.        tinkering, you can replace it with your known good copy. More 
  151.        about AUTOEXEC.BAT later in this tutorial. 
  152.  
  153.        We could also have named the batch file above hello.bat or 
  154.        info.bat rather than blink.bat by changing the first line we 
  155.        typed, but for simplicity we'll stick with blink.bat which does 
  156.        the following chores: Print "hello there" on the screen, then 
  157.        type the DOS version in use then display date and finally 
  158.        produce a directory listing with pause after each screenful. At 
  159.        this point the batch file ends and returns you to DOS. In the 
  160.        first line we use COPY CON as our small word processor to begin 
  161.        construction of the batch file named blink.bat. In the last line 
  162.        the ^Z means end of batch file preparation - exit back to DOS 
  163.        and save the file on disk. 
  164.  
  165.        Another example batch file for you to try, let's call it F.BAT
  166.        This is a reminder that F.BAT refers to formatting a disk:
  167.  
  168.        echo off                                        
  169.        copy con f.bat                                                 
  170.        cls                                                            
  171.        pause                                                          
  172.        format b:                                                      
  173.        echo all done                                                  
  174.  
  175.        Notice that here I have omitted the COPY CON command to start 
  176.        file preparation and the F6 to end file preparation. Use the 
  177.        COPY CON method described above if you wish or whatever word 
  178.        processing software is available to construct the file. 
  179.        
  180.        This batch file (activated by typing f then enter) will clear 
  181.        the screen then prepare to format a blank disk in b: drive. 
  182.        NOTE: you MUST have FORMAT.COM, the DOS formatting utility, on 
  183.        the same disk as the batch file, f.bat - remember that format is 
  184.        an EXTERNAL command and f.bat will try to find FORMAT.COM. 
  185.        
  186.        After the batch file has formatted the disk it prints "all done" 
  187.        on the screen. So instead of LOTS of keystrokes to format a 
  188.        disk, you just tap "F" then hit enter and the batch file runs. 
  189.        See how we are saving keystrokes - that's one of the purposes of 
  190.        a batch file! We will discuss the new ECHO command a little 
  191.        later in this tutorial. 
  192.        
  193.        A reminder: Ctrl-Break or Ctrl-C key combination will halt 
  194.        any batch file operation if you wish.
  195.  
  196.        The next batch file might be used to backup word processing data 
  197.        files from your hard drive onto a floppy disk. Let's make an 
  198.        initial assumption that your word processing documents are 
  199.        stored on your hard drive in the subdirectory C:\DOC. You could 
  200.        name this backup batch file B.BAT and when you need to backup 
  201.        simply type B (then press enter) at the DOS prompt. We've 
  202.        omitted the copy con command at the top of the file and the ^Z 
  203.        at the end of the file since you already know how to start and 
  204.        end a batch file from previous examples. Note the new commands 
  205.        we are using: ECHO and PAUSE which we will discuss shortly. 
  206.  
  207.        ECHO OFF
  208.        ECHO This batch file backs up DOCUMENT files to disk B:
  209.        ECHO READY TO BACKUP. 
  210.        PAUSE                                                                  
  211.        COPY C:\DOC\*.* B:
  212.        ECHO All done!                                                          
  213.  
  214.        The line which does most of the work is COPY C:\DOC\*.* B: 
  215.        which translates as "copy all files from C:\DOC subdirectory 
  216.        and transfer them to B: drive."
  217.  
  218.        One batch file can start or call another, but the original batch 
  219.        file cannot usually be returned to - you must continue on within 
  220.        the second batch file. For example, you could have one batch 
  221.        file start another batch file.
  222.  
  223.        If a batch file contains a typing or syntax error in any of its 
  224.        commands, the computer will stop execution at that point and 
  225.        return you to DOS which remembers which disk contains the batch 
  226.        file and the drive it was in. If you remove the original disk, 
  227.        DOS will ask you to replace it so it can finish executing the 
  228.        batch file. Batch files execute one step at a time from the disk 
  229.        and NOT from RAM memory. This disk-based nature of batch files 
  230.        make them a little slow, but they get the work done in 
  231.        reasonably short order for most people.
  232.  
  233.        Several books and power user tricks should also be mentioned 
  234.        regarding batch files before we move on . . .
  235.  
  236.        A superlative book on batch files you might wish to investigate 
  237.        is MOS-DOS Batch File programming by Ronny Richardson, 1988,
  238.        Wincrest Books.
  239.       
  240.        You should also investigate the SEBFU (Scanlon Enterprises Batch 
  241.        File Utilities) software package which is a series of small 
  242.        batch file utilities which offer an improvement over the 
  243.        standalone DOS batch file programming language. SEBFU allows the 
  244.        user to produce subtle, powerful batch files and includes an 
  245.        excellent tutorial about using batch files for productivity. If 
  246.        you wish to try SEBFU, the shareware version, contact Scanlon 
  247.        Enterprises, 38354 17th ST E #C, Palmdale, CA 93550 Telephone 
  248.        (805) 272-4827. Include five dollars for shipping and handling. 
  249.        
  250.        Special batch file COMPILER utilities exist which speed 
  251.        execution of batch files and make them run from RAM memory 
  252.        rather than disk. Most computer clubs and BBS system carry these 
  253.        batch file compilers. One popular batch compiler is named 
  254.        BAT2EXEC and was produced by PC Magazine several years ago. 
  255.        Batch files will FLY once they have been compiled and run 
  256.        from memory rather than disk. Another batch file speedup trick 
  257.        uses a "ramdisk" as follows.
  258.  
  259.        Remember the DOS VDISK command in our second DOS tutorial? Many 
  260.        DOS experts put commonly used batch files in a virtual or RAM 
  261.        disk in memory where a batch file runs quickly. This is one 
  262.        trick which can turbocharge batch file operations. 
  263.  
  264.        ----------------------------------------------------------------
  265.  
  266.           AUTOEXEC.BAT FILE BASICS - THE WAKEUP CALL TO YOUR COMPUTER 
  267.        
  268.        ----------------------------------------------------------------
  269.       
  270.        The AUTOEXEC.BAT file starts your computer exactly the way you 
  271.        want. It allows you to customize the machine to your liking as 
  272.        the computer comes to life. You can cause the AUTOEXEC.BAT file 
  273.        to print a startup menu of choices, load one particular program, 
  274.        execute another batch file or other useful tasks. The 
  275.        AUTOEXEC.BAT file is the first file DOS runs after loading 
  276.        itself and configuring the computer. The AUTOEXEC.BAT file must 
  277.        be on the same disk as DOS when the computer starts. 
  278.  
  279.        The AUTOEXEC.BAT file is a special batch file which MUST be 
  280.        placed in the main or root directory of a disk to function 
  281.        properly. 
  282.  
  283.        An AUTOEXEC.BAT file can always be modified, enlarged, edited, 
  284.        or deleted later as you wish. Sometimes it is useful to have 
  285.        several AUTOEXEC.BAT files. Each on a different startup disk to 
  286.        operate different programs! An AUTOEXEC.BAT file, like all batch 
  287.        files, can be modified with any word processor, DOS EDIT or 
  288.        EDLIN text editor. 
  289.  
  290.        Before tinkering with your AUTOEXEC.BAT file, make sure you do 
  291.        not accidentally over-write or destroy your current AUTOEXEC.BAT 
  292.        file. If necessary, rename your current AUTOEXEC.BAT file (using 
  293.        the rename or REN command) and make a new file while saving the 
  294.        old one "just in case." Never edit files on your original DOS disk, 
  295.        work on a copy! This wise advice applies to ANY computer file.
  296.        Save a backup copy; never work on the original.
  297.  
  298.        Examine the next batch file:
  299.  
  300.        copy con AUTOEXEC.BAT              (press enter)
  301.        123                                (press enter)
  302.        ^Z                                 (press enter)
  303.  
  304.        This means (first line) create a file named AUTOEXEC.BAT as 
  305.        typed from the keyboard or con (console). Then (second line) 
  306.        start program named 123. The (final line) end of batch file 
  307.        preparation - stash it on the disk. Since the first and last 
  308.        lines prepare the batch file, this program really has only one 
  309.        line whose purpose is to start a specific program (123.EXE) each 
  310.        time the computer is turned on.
  311.  
  312.        When finished you'll see a file named AUTOEXEC.BAT on your 
  313.        directory listing screen which contains automatic startup 
  314.        instructions. If this file were placed on your main DOS disk it 
  315.        would try to start a program such as 123.EXE if such a program 
  316.        existed there. And since it is AUTOEXEC.BAT this would be the 
  317.        first file run each morning when you turn on your computer.
  318.  
  319.        You can also start the AUTOEXEC.BAT by typing autoexec and then 
  320.        pressing enter. To take a "peek" at the contents of an 
  321.        AUTOEXEC.BAT file (or any bat file) simply use the type command. 
  322.        Remember to use Ctrl-S key combination to pause the screen if 
  323.        the display flashes by too quickly. 
  324.  
  325.        Example: C>type AUTOEXEC.BAT       (display file contents)
  326.        Example: A>type b:AUTOEXEC.BAT     (display file on the B: drive)
  327.        Example: C>type AUTOEXEC.BAT>PRN   (display file contents on 
  328.                                            printer)
  329.        
  330.        Here is another AUTOEXEC.BAT file, this time from a computer 
  331.        equipped with a hard drive. This example provides a higher 
  332.        degree of control and direction that a computer user might need 
  333.        for hard drive customization. 
  334.  
  335.        path \dos;\reflex;\wp;\util;\doc;\nor;\bat                     
  336.        prompt $P$G                                                    
  337.        cpu n                                                          
  338.        verify on                                                      
  339.        blank                                                          
  340.        mode bw80,r                                                    
  341.        dispclk                                                        
  342.        type menu.txt                                                  
  343.  
  344.        Let's examine this more complicated AUTOEXEC.BAT file in greater 
  345.        detail: 
  346.  
  347.        The first line after establishes a path command to help DOS 
  348.        search every subdirectory on the hard disk -you don't have to 
  349.        switch around to different areas of the disk, DOS will search 
  350.        for you since it knows the various subdirectory "paths" to take. 
  351.  
  352.        The second line alters the cursor prompt to always display your 
  353.        current location and subdirectory. Instead of seeing C> you view
  354.        a more informative C:\DOCS> for example.
  355.  
  356.        The third line is a reference to the speed the computer will 
  357.        operate at and is a unique command to a particular brand of 
  358.        machine (cpu n means start the central processing unit chip at 
  359.        normal speed.) Cpu is really CPU.COM, an external file which 
  360.        sets the computer's processing speed. Your DOS disk may or may 
  361.        not contain the file CPU.COM. This highlights the ability of the 
  362.        AUTOEXEC.BAT file to start or load other programs and is 
  363.        very useful!
  364.  
  365.        The fourth line turns on the verify function for file copying. 
  366.  
  367.        The next line instructs the DOS mode function to switch to black 
  368.        and white display, 80 columns wide and shift one column to the 
  369.        right for alignment. We are setting the hardware the way we 
  370.        wish. We could also configure the modem or printer with the mode 
  371.        command.
  372.  
  373.        Next we ask DOS to tell us the time and date. Run the program 
  374.        DISPCLK.COM, an external program stored on disk.
  375.   
  376.        The final line instructs DOS to type to the screen a text file 
  377.        containing a simple menu for the monitor to display. Menu.txt
  378.        probably gives us choices of programs and thus calls other batch 
  379.        files. 
  380.  
  381.        ---------------------------------------------------------------- 
  382.  
  383.                           BATCH FILE COMMANDS AND USE 
  384.  
  385.        ---------------------------------------------------------------- 
  386.  
  387.        In addition to the normal DOS commands, batch files have eight 
  388.        special subcommands. At the end of this section we will provide 
  389.        some interesting batch files which you can use or edit on your 
  390.        computer. 
  391.        
  392.        The special batch commands are: 
  393.  
  394.        --- REM --- The REM command simply documents or notes a part of 
  395.        a batch file's operation for your reference. You should use REM 
  396.        extensively to document long detailed batch files so you can 
  397.        locate portions of the program if you decide to change the batch 
  398.        file later. Remarks can be up to 123 characters long. REM does 
  399.        not cause any operation, it merely documents what you want to 
  400.        say or do. 
  401.  
  402.        Example: C>REM this is the location of menu operations 
  403.  
  404.        In DOS 2.0 the REM command could be replaced with a period or 
  405.        dot, but this is not true in DOS 3.0 and above. 
  406.  
  407.        Example:  C>. this is the location of menu operations 
  408.  
  409.        --- PAUSE --- Stops batch file execution on a temporary basis 
  410.        until you press a key. Thus you can pause a batch file and do 
  411.        some operation (perhaps changing a floppy disk) and then 
  412.        continue when you strike a key. Very useful.
  413.  
  414.        Example: B>PAUSE   
  415.        Example: B>PAUSE This is an optional message, pardner! 
  416.  
  417.        In the first example, no message is displayed.
  418.  
  419.        --- ECHO --- Turns display listing of commands on/off. It can 
  420.        also send a message to the screen. It is frequently turned off 
  421.        to remove excessive screen messages. Normally, with ECHO on, 
  422.        screen messages are sent to the screen which can be distracting. 
  423.        To suppress them use the first example. To restart the messages 
  424.        use the second example. To add a message with the ECHO command 
  425.        see example three. REM or remark command can also send a message 
  426.        to the screen but NOT with ECHO turned off! 
  427.  
  428.        Example:   A>ECHO OFF 
  429.        Example:   A>ECHO ON 
  430.        Example:   A>ECHO It's raining cats, dogs and computers
  431.        Example:   A>@ECHO (don't display this particular line)
  432.        
  433.        --- PARAMETERS AND MARKERS ----  This is NOT a batch file 
  434.        command like ECHO or PAUSE. 
  435.  
  436.        Instead parameters are additional pieces of information or 
  437.        "modifiers" which follow DOS commands. 
  438.  
  439.        Example:   C>format b:/s     
  440.  
  441.        In the above, format is the command while b: and /s are the 
  442.        parameters. Parameters modify the basic operation of a DOS 
  443.        command but are not required by the command to operate. A batch 
  444.        file can also accept parameters such as a word, filename, 
  445.        symbol, drive letter or any useful character or group of 
  446.        characters! 
  447.  
  448.        Markers placed inside the batch file listing signify which 
  449.        parameter goes where. Markers are made from a percent sign (%) 
  450.        and a single digit between 0 and 9 for a total of ten markers 
  451.        available (remember, zero is a number too.) Here are the ten 
  452.        markers: 
  453.  
  454.        %0    %1    %2    %3    %4    %5    %6    %7    %8    %9 
  455.  
  456.        Let's use an example. Pretend that DOLITTLE.BAT is on your 
  457.        floppy. Within its listing of commands there might be this 
  458.        single line: 
  459.  
  460.        ECHO %0 %1 %2      (ECHO shows messages on the monitor) 
  461.  
  462.        If at the DOS prompt you typed:  
  463.        
  464.        B>DOLITTLE fancy pants  (then press enter) 
  465.  
  466.        Your screen would show the following:   
  467.                   
  468.                           ECHO DOLITTLE fancy pants. 
  469.  
  470.        In this case, %0 has taken on the value at the start of the DOS 
  471.        command which is the first word "DOLITTLE". Meanwhile %1 has 
  472.        become "fancy" and %2 is now pants. 
  473.  
  474.        Looking at this another way: 
  475.  
  476.                         DOLITTLE   fancy  pants 
  477.                          |          |       | 
  478.                  ECHO    %0         %1      %2    
  479.  
  480.        Let's try a more useful example. Pretend you had a large file of 
  481.        word processing files containing bills you have to pay from time to 
  482.        time. 
  483.  
  484.        You need to look up bills or amounts in the file accounts.txt 
  485.        which is in plain ASCII (english) text from your word processor. 
  486.        
  487.        The DOS FIND utility can search large files for specific words, 
  488.        strings or characters. The general format for the FIND command 
  489.        is: FIND "text" filename. FIND is located in the file FIND.COM
  490.        on your DOS disk and must be present with the batch file to be
  491.        used.
  492.  
  493.        A simple batch file possibly named GET.BAT could do this: 
  494.        
  495.        ECHO OFF
  496.        ECHO searching for data . . . . 
  497.        FIND "%1" %2 
  498.        ECHO Finished, boss 
  499.  
  500.        Start the batch file get.bat with search data like this: 
  501.  
  502.        C>get grocery accounts.txt    (first word starts get.bat, second 
  503.        word is the item to search for, third item is the file to 
  504.        search.) 
  505.        
  506.        As a result, you will get a report of the line where the word 
  507.        "grocery" is found within the file accounts.txt. This could also 
  508.        be used to search a telephone list or list of employee names and 
  509.        addresses. A powerful idea for a short batch file! 
  510.  
  511.        --- GOTO ---  Jumps to a labeled set of commands within the 
  512.        batch file. The general format for the command is    GOTO LABEL 
  513.        where LABEL is a line in the batch file which must start with a 
  514.        colon (:) followed by a name up to eight characters long. 
  515.  
  516.        A simple, but useless batch file illustrates the GOTO command by 
  517.        looping around in circles doing the same task endlessly.            
  518.                                                             
  519.        Example listing for batch file: 
  520.  
  521.        ECHO OFF
  522.        :kitty 
  523.        ECHO watch this fill your screen over and over, folks 
  524.        GOTO kitty 
  525.  
  526.        Note! On some versions of DOS it is necessary to include one blank 
  527.        line at the end of this file. In the above example, just press 
  528.        Enter/Return key one extra time after the line "GOTO kitty"
  529.        and then save the batch file.
  530.  
  531.        The above batch file will continue to print the ECHO line over 
  532.        and over since it always returns to the start. Tap Ctrl-Break to 
  533.        stop this silliness. The true usefulness of the GOTO command is 
  534.        best understood by allowing the GOTO within a batch file to 
  535.        transfer control elsewhere within its listing rather than to the 
  536.        line immediately next in sequence. You can thus cause varying 
  537.        useful results depending on a conditions present. Choices and 
  538.        different outcomes are a trademark of savvy batch file use.
  539.  
  540.        --- IF ---  Allows conditional operation of a command. This is a 
  541.        fancy way of saying you can cause a batch file to make decisions 
  542.        based on a logical condition or input then do something. The 
  543.        usual syntax of the IF command is IF CONDITION COMMAND. Let's 
  544.        take this apart and examine the concept. 
  545.  
  546.        In the situation IF CONDITION COMMAND: 
  547.  
  548.        COMMAND is any normal DOS or batch file command and CONDITION is 
  549.        one of three possible tests that yield true or false.         
  550.  
  551.        Example:  IF %1==w GOTO dog        (we'll explain this in a bit) 
  552.        Example:  IF %3 == 80 MODE BW80    (we'll explain this in a bit) 
  553.  
  554.        The three possible tests are: 
  555.  
  556.        1. The ERRORLEVEL condition (i.e., a specific number is found). 
  557.        2. The STRING COMPARISON. (i.e., two strings are equivalent or 
  558.        not.) 
  559.        3. The FILE EXISTENCE condition. (i.e., if a file exists or not.) 
  560.  
  561.        In true full-featured programming languages many other logical 
  562.        tests might be allowed, but for batch files these are the 
  563.        only three tests. Let's examine the three more closely. Then 
  564.        illustrate with an example.
  565.  
  566.        1. ERRORLEVEL is a number which tells DOS whether the last 
  567.        program run was successful. If so the errorlevel is zero (0) 
  568.        anything else above zero means unsuccessful. 
  569.  
  570.        2. STRING COMPARISON, the second conditional test, is always 
  571.        indicated in a batch file by double equals signs (==). A test is 
  572.        designated by the condition  IF string1 == string2. This is 
  573.        frequently used with parameters or markers such as:  IF %3 == 80 
  574.        MODE BW80. 
  575.  
  576.        3. In the final and third conditional test, FILE EXISTENCE, the 
  577.        usual format is IF EXIST d:filename.ext. which checks for a 
  578.        certain file on a certain drive. You can thus check for a 
  579.        certain disk or file before continuing the batch file process. 
  580.        Pathnames are not allowed (d:\slip\and\slide). 
  581.  
  582.        Let's try a batch file example to illustrate the use of STRING 
  583.        COMPARISONS to make a choice in how the batch file does its 
  584.        work. In a way, this is a menu program. Pretend you have two 
  585.        software applications. One is a word processor named WORD.EXE 
  586.        whose command to start is WORD and the other is a spreadsheet 
  587.        named LOTUS.EXE whose command is LOTUS to start. 
  588.  
  589.        If we prepared a simple batch file called go.bat whose listing is 
  590.        below, we could start one or the other program by using either 
  591.        the command:  
  592.  
  593.                      A>go w  (to start the word processor)  
  594.  
  595.                                    OR THIS:
  596.  
  597.                       A>go s  (to start the spreadsheet). 
  598.  
  599.        Notice how the "w" or "s" is picked up by the batch file and 
  600.        sends the program either one direction or the other in the 
  601.        example below. The remarks lines which begin with REM in the 
  602.        batch file give you a clue about the operation of the program 
  603.        but are not themselves commands. The end result of this batch 
  604.        file is a saving of keystrokes for frequently used software (the 
  605.        word processor and spreadsheet) and could be expanded to start 
  606.        many other software packages. 
  607.       
  608.        REM This batch file selects one of two choices based on input
  609.        REM The next line turns off screen echo to avoid screen clutter
  610.        ECHO OFF
  611.        REM Begin test for one of two choices
  612.        REM Next two lines use percent signs as markers for "w" or "s" keys 
  613.        IF %1==w GOTO dog
  614.        IF %1==s GOTO cat
  615.        REM Next line forces goto end if no match is made for w or s 
  616.        GOTO end
  617.        :dog
  618.        REM Next command starts word processor, WORD.EXE
  619.        WORD
  620.        GOTO end
  621.        :cat
  622.        REM Next command starts spreadsheet, LOTUS.EXE
  623.        LOTUS
  624.        GOTO end
  625.        :end
  626.        REM Next line switches to root directory and ends the batch file
  627.        CD\   
  628.        ECHO Batch file done, bye bye!
  629.        
  630.        --- SHIFT ---  Re-assigns the relationship of parameters to 
  631.        markers. It changes their values. And it does it in a very odd 
  632.        way . . . 
  633.  
  634.        Remember that there are only ten markers available to a batch 
  635.        file to hold the parameter values as we mentioned above. Here 
  636.        they are: 
  637.  
  638.        %0    %1    %2    %3    %4    %5    %6    %7    %8    %9 
  639.  
  640.        However you can raise the limit of 10 parameters in a batch file 
  641.        using the single word SHIFT. When this command is encountered in 
  642.        a batch file, all the parameter and marker pairings are shifted 
  643.        one unit to the left. Whatever was assigned to %0 is lost. 
  644.  
  645.        A diagram to visualize. Before a SHIFT command is issued the 
  646.        parameters and markers might be: 
  647.  
  648.                 %0    %1    %2    
  649.                  |     |     |
  650.                 dog   cat    computer
  651.  
  652.        After the SHIFT command we would see:
  653.  
  654.                 %0    %1          %2    
  655.                  |     |          |
  656.                 cat   computer
  657.  
  658.        Notice that dog is lost, %1 becomes computer and %2 is left 
  659.        vacant unless it takes a new parameter from %3 (if %3 had a 
  660.        parameter). The effects of the SHIFT command are wide ranging 
  661.        throughout the batch file and provide great flexibility and a 
  662.        range of parameters greater than ten values. 
  663.  
  664.        --- FOR..IN..DO ---    Allows iteration (repetition) of actions 
  665.        or commands. The command is similar to a FOR...NEXT...STEP loop 
  666.        programmers use. This command lets you repeat an action several 
  667.        times.
  668.  
  669.        The command is rather subtle and could be thought of as a three 
  670.        part command. The syntax is: 
  671.  
  672.        FOR %%Variable IN (Set) DO Command 
  673.  
  674.        Let's look more closely at the three parts: 
  675.  
  676.           FOR %%Variable       IN (Set)    DO Command
  677.           ==============       =======     ==========
  678.               |                    |           |         
  679.             part 1              part 2     part 3    
  680.  
  681.        Translating into English this means: FOR a certain batch file 
  682.        variable withIN a SET of filenames or commands DO a certain 
  683.        action. 
  684.  
  685.        The %%VARIABLE is a one-letter variable which must have a double 
  686.        %% prior to the letter to distinguish it from single % markers 
  687.        we have seen earlier. 
  688.  
  689.        The SET portion of the command is always in parenthesis as 
  690.        (SET). The SET represents filenames or DOS commands you want the 
  691.        %% variable to assume while the command is executing. A space is 
  692.        used between entries. Pathnames are never allowed but wildcards 
  693.        such as *.* are acceptable. If the SET contains DOS command then 
  694.        only the %%VARIABLE is used. 
  695.               
  696.        The COMMAND is a DOS command or batch subcommand. One or several 
  697.        of these commands will contain the %%Variable in it. 
  698.  
  699.        Let's try an example. Pretend by you want a batch file to 
  700.        present the DOS version then clear the screen and finally issue 
  701.        the directory. We could do this in three lines by: 
  702.        
  703.        VER
  704.        CLS
  705.        DIR/P
  706.        
  707.        However, with the command FOR..IN..DO we can do this in one 
  708.        line:
  709.  
  710.        FOR %%T IN (Ver cls Dir/P) DO %%T 
  711.  
  712.        Notice how each DOS command is separated by a space. ? and * are 
  713.        NOT allowed within any command within the SET. Use a colon : 
  714.        instead of a space within the set when passing parameters to 
  715.        programs. You can issue the FOR..IN..DO batch file subcommand at 
  716.        the DOS prompt by dropping one of the percentage signs  %  on 
  717.        the variable. Let's move on to some practical and fairly 
  718.        interesting examples . . . 
  719.  
  720.        ---------------------------------------------------------------- 
  721.  
  722.                       BATCH FILE PROJECTS FOR YOU TO TRY!
  723.  
  724.        ---------------------------------------------------------------- 
  725.  
  726.        The following batch files do real work and can teach you some 
  727.        fascinating principles. 
  728.        
  729.        To save typing each example, here's a shortcut: simply load this 
  730.        file, BATCH.TUT, from the PC-Learn disk or hard drive into your 
  731.        word processor. Next, delete the tutorial and batch files you 
  732.        don't need, keeping only the batch file lines you require. Move 
  733.        all lines of the batch file to the far left margin then save the 
  734.        batch file back to disk in plain ASCII text with a permanent 
  735.        batch file name such as CANDY.BAT, GO.BAT or PRINTER.BAT which 
  736.        you can easily remember. Feel free to change or add things to 
  737.        these batch files with your word processor since that's the 
  738.        point - batch files are flexible! 
  739.        
  740.        Note the liberal use of the remarks or REM lines in some batch 
  741.        files to explain how things work. If you like, delete all REM 
  742.        lines to save typing time, space and speed program execution! 
  743.  
  744.                       ---- Fast freespace batch file ----
  745.        
  746.        This is a short but useful batch file. It reports the amount of 
  747.        freespace on a disk by using the FIND command in DOS to search 
  748.        out the line containing the word "free" in the DOS DIR command.
  749.  
  750.                             Program name: TELLFREE.BAT
  751.        
  752.        ECHO OFF
  753.        CLS
  754.        ECHO CHECKING FOR FREE SPACE ON DISK
  755.        DIR | FIND "free"
  756.  
  757.        You could modify the last line to DIR A: | FIND "free" to locate 
  758.        the free space on the A: drive. The piping symbol | , discussed 
  759.        in a previous DOS tutorial illustrates how one command (DIR) can 
  760.        "pipe" its output into the FIND command. You MUST have the DOS 
  761.        file FIND.EXE on the same disk so that the DIR command can use 
  762.        it. The word "free" is case sensitive and must MOT be typed in 
  763.        capital letters since the FIND command is case sensitive. 
  764.        
  765.        Obviously, to use this small program, at the DOS prompt just 
  766.        type TELLFREE then press the return or enter key. You could also 
  767.        name this batch file T.BAT if you wanted to only type a 
  768.        single letter and save additional keystrokes. 
  769.  
  770.                       ---- Fast deletion batch file ----
  771.        
  772.        This is a short batch file with some powerful wrinkles for 
  773.        speedy file deletions. It could be modified in many ways.
  774.  
  775.                               Program name: DB.BAT
  776.        
  777.        ECHO OFF
  778.        CLS
  779.        ECHO Ready to delete ALL files on B: drive
  780.        ECHO Press control-break keys to abort or
  781.        ECHO any other key to continue
  782.        PAUSE
  783.        REM Following line does the deleting
  784.        ECHO Y | DEL B:*.*
  785.        ECHO DONE!
  786.  
  787.        This is an odd batch file using some powerful DOS secrets. Line 
  788.        four reminds us we are about to delete all files on the B: drive 
  789.        and gives us the chance to abort using the control-break key 
  790.        combination which works to abort all batch files. The pause 
  791.        command on line six halts operations and waits for our keypress. 
  792.        
  793.        Line eight shows the real power of DOS in a one line command 
  794.        which uses ECHO to pass the keystroke Y (meaning yes) via the 
  795.        pipe operation of DOS represented by the vertical bar |. This 
  796.        effectively means that the deletion of all files on B: drive 
  797.        represented by *.* will take place WITHOUT pausing for the 
  798.        traditional Yes/No request. The name of the batch file, DB.BAT 
  799.        reminds us that its use is to delete all files on B: drive or 
  800.        DB, for short! 
  801.               
  802.                       ---- Fast formatting batch file ----
  803.        
  804.        This batch file will speed your disk formatting. Its name 
  805.        F.BAT means format disks, and it gives you some choices and 
  806.        illustrates other batch file tricks.
  807.        
  808.        There are three ways to use it: for formatting A: drive, B: 
  809.        drive, or both A: and B: drives. Note the minimum number of 
  810.        keystrokes required and how the batch file determines your 
  811.        choice by using parameters. This batch file also requires a 
  812.        small text file called yes.txt which shows how a file can be 
  813.        used to redirect input to a DOS command in place of the 
  814.        keyboard. It is explained below. Be sure to prepare YES.TXT or 
  815.        F.BAT will not work!
  816.  
  817.        To start this batch file you MUST chose one of the following. 
  818.        The space between letter characters is important.
  819.  
  820.        To format only A: drive, at DOS prompt enter   F A
  821.        To format only B: drive, at DOS prompt enter   F B
  822.        To format both A and B drives, at DOS prompt enter   F AB
  823.        
  824.        In the above commands, the A, B or AB will be inserted into the 
  825.        batch file in the location of the symbol %1 as noted earlier in 
  826.        this tutorial.
  827.        
  828.                              Program name: F.BAT
  829.        
  830.        ECHO OFF
  831.        CLS
  832.        ECHO FORMATTING DISKS NOW! 
  833.        REM This batch file selects one of three choices based on input
  834.        REM Next line begins test for one of three choices
  835.        IF %1==A GOTO DOG
  836.        IF %1==B GOTO CAT
  837.        IF %1==AB GOTO MOUSE
  838.        REM Next line forces goto end if no match is made 
  839.        GOTO end
  840.        :DOG
  841.        FORMAT A:<yes.txt
  842.        GOTO end
  843.        :CAT
  844.        FORMAT B:<yes.txt
  845.        GOTO end
  846.        :MOUSE
  847.        FORMAT A:<yes.txt
  848.        FORMAT B:<yes.txt
  849.        GOTO end
  850.        :end
  851.        
  852.        Lines 18 and 19 provide formatting of B: immediately after 
  853.        formatting of A: is finished. A short but very powerful batch 
  854.        file. 
  855.        
  856.        Note, how the batch file cleverly uses redirection with the 
  857.        < symbol discussed in our earlier DOS tutorial to send the 
  858.        "stored keystrokes" in the file yes.txt to the format command so 
  859.        you do NOT need to type Yes/No each time the computer formats a 
  860.        disk which is usual when using the format command. Redirection 
  861.        is a very powerful DOS operation. You will also need the file 
  862.        YES.TXT as discussed below. 
  863.  
  864.                              Program name: YES.TXT
  865.  
  866.        This is a very simple file. Start your word processor, DOS Edlin 
  867.        or even use the COPY CONsole command earlier. This tiny file 
  868.        contain a "y" character and two carriage returns and serves as 
  869.        an input file to take the place of the keyboard as explained 
  870.        above. It provides the format command with "simulated" keyboard 
  871.        response of Y (Yes) so the batch file above can continue.
  872.  
  873.        Using Copy Console command (which is one method to prepare
  874.        YES.TXT):
  875.        
  876.        COPY CON YES.TXT     (press enter)
  877.        y                    (press enter)
  878.                             (press enter, skip to new line)
  879.                             (press enter, skip to new line)
  880.        ^Z                   (press F6 to end and write file to disk)
  881.        
  882.                    ---- Make a menu batch file project ----
  883.        
  884.        The next project is actually four simple batch files which work 
  885.        together to provide a "poor man's" menu program for a hard 
  886.        drive. Note the automatic switching between subdirectories 
  887.        provided by the files 1.bat, 2.bat and 3.bat. Obviously you 
  888.        could expand this to include menu choices for more options. Type 
  889.        in each batch file program and save on your hard drive. Place 
  890.        all files in the root directory of your hard drive, usually C:\ 
  891.        When ready to start, simply type M, then press enter key. Notice 
  892.        how M.BAT runs one of the other three files, 1.BAT, 2.BAT or 
  893.        3.BAT. Notice also how when those batch files are done, they 
  894.        automatically run M.BAT to return from where they began and 
  895.        again display the menu screen.
  896.  
  897.                               Program name: M.BAT
  898.        
  899.        REM Third line turns off echo so commands are not repeated to
  900.        REM the screen twice, avoids screen clutter
  901.        ECHO OFF
  902.        REM Next line clears the screen
  903.        CLS
  904.        REM Next line suggests option 1 which will run 1.bat 
  905.        ECHO PRESS 1 FOR WORD PROCESSOR
  906.        REM Next line suggests option 2 which will run 2.bat 
  907.        ECHO PRESS 2 FOR SPREADSHEET
  908.        REM Next line suggests option 3 which will run 3.bat 
  909.        ECHO PRESS 3 FOR DATABASE
  910.        ECHO PRESS ENTER KEY AFTER SELECTION IS MADE
  911.  
  912.                               Program name: 1.BAT
  913.  
  914.        ECHO OFF
  915.        REM Fourth line switches to a subdirectory containing your
  916.        REM word processor using the cd or change directory command
  917.        CD\WP
  918.        REM Seventh line starts your word processor, named word.exe
  919.        REM Substitute the startup command for your word processor
  920.        WORD
  921.        REM Eleventh line changes out of word processing subdirectory 
  922.        REM and goes back to the root directory when word processor
  923.        REM is finished
  924.        CD\
  925.        REM Final line restarts the menu program m.bat
  926.        M
  927.        
  928.                               Program name: 2.BAT
  929.  
  930.        ECHO OFF
  931.        REM Fourth line switches to a subdirectory containing your
  932.        REM spreadsheet using the cd or change directory command
  933.        CD\SPREAD
  934.        REM Seventh line starts your spreadsheet, named lotus.exe
  935.        REM Substitute the start command for your spreadsheet
  936.        LOTUS
  937.        REM Eleventh line changes out of spreadsheet subdirectory
  938.        REM and goes back to the root directory when spreadsheet
  939.        REM is finished
  940.        CD\
  941.        REM Final line restarts the menu program m.bat
  942.        M
  943.        
  944.                               Program name: 3.BAT
  945.        
  946.        ECHO OFF
  947.        REM Fourth line switches to a subdirectory containing your
  948.        REM database using the cd or change directory command
  949.        CD\DATABASE
  950.        REM Seventh line starts your database, named db.exe
  951.        REM Substitute the start command for your database
  952.        DB
  953.        REM Eleventh line changes out of database subdirectory
  954.        REM and goes back to the root directory when database
  955.        REM IS FINISHED
  956.        CD\
  957.        REM Final line restarts the menu program m.bat
  958.        M
  959.        
  960.                       ---- Printer Control Batch file ----
  961.        
  962.        Some of the best kept DOS secrets are fascinating. If you have a 
  963.        standard dot matrix printer connected to your computer, you can 
  964.        use simple batch files to change the typeface and other features 
  965.        your printer provides. The reference booklet which accompanied 
  966.        your printer discusses printer control codes. For example, on 
  967.        Epson compatible printers you will note in your printer book 
  968.        that "emphasized printing" has the DECIMAL control code 27 69. 
  969.        Let's write a batch file to take charge of our printer! 
  970.        
  971.                               Program name: E.BAT
  972.  
  973.        ECHO OFF
  974.        CLS
  975.        ECHO SETTING PRINTER TO EMPHASIZED MODE
  976.        ECHO (ALT 155)(ALT 69) >PRN
  977.        ECHO DONE
  978.  
  979.        The fourth line contains a secret trick. The code we need to 
  980.        send is 27 69 according to our printer book. When preparing this 
  981.        batch file with EDLIN, COPY CONsole or your word processor, you 
  982.        must send the printer control code 27 then 69 to the printer. 
  983.        
  984.        In line four after typing the word "ECHO" then a blank space, 
  985.        hold down the ALT key then press 155 on the FAR RIGHT NUMERIC 
  986.        KEYBOARD. When done entering the number, release the ALT key. 
  987.        On most computers, the cents symbol will appear which the 
  988.        printer will accept as the "escape code 27." DO NOT type the 
  989.        left and right parenthesis marks which appear on line four: ( ), 
  990.        they are only for clarity. 
  991.  
  992.        Next press ALT 69 which produces the E symbol. You could also 
  993.        just type capital E. Next type >PRN which sends this code 
  994.        to your printer. Note that there is NO blank space between (ALT 
  995.        155) and (ALT 69). 
  996.  
  997.        The code 155 is substituted for 27 but the 69 is unchanged. Why 
  998.        155 rather than 27 for the escape code? An explanation: Printer 
  999.        control codes begin with code 128, thus escape character 27 is 
  1000.        generated by using 27+128 = 155. An necessary trick for this 
  1001.        batch file. We could send SEVERAL codes by adding more lines to 
  1002.        the batch file to set letter quality, pica font, line spacing 
  1003.        and tabs, then type the letter to the printer (ECHO LETTER.TXT>PRN) 
  1004.        and finally reset the printer. Your printer book discusses these 
  1005.        decimal control codes. 
  1006.  
  1007.        If we wanted to send a formfeed to the printer (eject paper,) 
  1008.        the printer reference book suggests control code 12, therefore 
  1009.        12+128 = 140. So the fourth line in the batch file would read 
  1010.        ECHO (ALT 140) >PRN. The bell sound (decimnal code 7) is 
  1011.        generated by 7+128 = 135. The batch file would change to read 
  1012.        ECHO (ALT 135) >PRN. 
  1013.        
  1014.        Some printer features are controlled by SINGLE control codes 
  1015.        while other features are controlled by MULTIPLE escape sequence 
  1016.        codes which always begin with 27 followed by additional numbers.
  1017.        Escape code 27 is always translated to ALT 155 when DOS and 
  1018.        batch files transmit the printer control information. Within 
  1019.        software programs the codes may use a different format: \027E
  1020.        for example.
  1021.  
  1022.        Many other printing features can be turned on using short batch 
  1023.        files. For example, double strike printing uses the decimal code 
  1024.        27 71. In the batch file you could use ECHO (ALT 155)(ALT 71) >PRN. 
  1025.        
  1026.        You can also turn on a COMBINATION of several features in one 
  1027.        longer batch file to control several features. Just add more 
  1028.        lines and codes to the batch file!
  1029.        
  1030.        To reset the printer when a printing job is finished: code 27 
  1031.        64. Search out these printer codes in your printer manual and 
  1032.        let a batch file do the hard work! 
  1033.  
  1034.        One of the best batch file tutorials to date was published in 
  1035.        two parts in the November and December 1991 editions of PC 
  1036.        Computing Magazine. Contact your library for back issues or 
  1037.        contact PC Computing at Back Issues Dept, PC Computing Magazine, 
  1038.        Ziff Davis, POB 53131, Boulder, CO 80322. Back issues currently 
  1039.        cost $6.00 each.
  1040.                          
  1041.        The bibliography/suggested reading list with PC-Learn provides 
  1042.        additional reading suggestions to advance your batch file 
  1043.        knowledge. 
  1044.  
  1045.        Tutorial finished. Have you registered PC-Learn to receive your
  1046.        bonus disks? Registration is encouraged. Shareware works on the
  1047.        honor system! Send $25 to Seattle Scientific Photography, 
  1048.        Department PCL6, PO Box 1506, Mercer Island, WA 98040. Latest 
  1049.        version of PC-Learn and two bonus disks shipped promptly!
  1050.        
  1051.        
  1052.